package com.lottery.ruok.lottrey.ui.component;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChuantongcaiActivity;
import com.lottery.ruok.lottrey.activity.LiuhecaiDetailActivity;
import com.lottery.ruok.lottrey.activity.OfficalLotteryDetailActivity;
import com.lottery.ruok.lottrey.adapter.HomeCPGridAdapter;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.NoScrollGridView;

public class HomeCPGroupView extends BaseRelativeLayout implements View.OnClickListener {
    @BindView(R.id.tv_pc_group_title)
    TextView mTitleView;
    @BindView(R.id.tv_pc_group_more)
    TextView mMoreView;
    @BindView(R.id.grid_item)
    NoScrollGridView mGridView;
    @BindView(R.id.iv_icon_color)
    ImageView ivIconColor;
    private HomeCPGridAdapter mAdapter;
    private MoreBuyLotteryClickListener listener;
    private GetWholeCategoryResponse data;

    public HomeCPGroupView(Context context) {
        super(context);
    }

    public HomeCPGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeCPGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        LayoutInflater.from(mContext).inflate(R.layout.view_home_cp_group, this);
        ButterKnife.bind(this, this);
        mAdapter = new HomeCPGridAdapter(mContext);
        mGridView.setAdapter(mAdapter);
        mMoreView.setOnClickListener(this);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                GetWholeCategoryResponse.ItemsBean itemsBean = mAdapter.getList().get(i);
                if (itemsBean != null) {
                    gotoLotteryDetailActivity(itemsBean.getCode(), itemsBean.getItem_class());
                }
            }
        });
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(GetWholeCategoryResponse data) {
        this.data = data;
        if (data != null) {
            if (mAdapter != null) {
                if (data.getItems().size()<=6){
                    mAdapter.setList(data.getItems());
                }else {
                    List< GetWholeCategoryResponse.ItemsBean> list=new ArrayList<>();
                    for (int i = 0; i < 6; i++) {
                        list.add(data.getItems().get(i));
                    }
                    mAdapter.setList(list);
                }

            }
            mTitleView.setText(data.getName());
            if ("传统彩".equals(data.getName())) {
                ivIconColor.setBackgroundColor(Color.parseColor("#ff7f02"));
            } else if ("官方彩".equals(data.getName())) {
                ivIconColor.setBackgroundColor(Color.parseColor("#ff3f3e"));
            } else if ("低频彩".equals(data.getName())) {
                ivIconColor.setBackgroundColor(Color.parseColor("#3d42f8"));
            } else if ("极速彩".equals(data.getName())) {
                ivIconColor.setBackgroundColor(Color.parseColor("#703eff"));
            }
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_pc_group_more:
                if (listener != null) {
                    listener.onClick(data);
                }
                break;
        }
    }

    public void gotoLotteryDetailActivity(String code, int index) {//0 其它 1官方2六合3传统
        switch (index) {
            case 1: {
                Intent intent = new Intent(mContext, OfficalLotteryDetailActivity.class);
                intent.putExtra("code", code);
                mContext.startActivity(intent);
                break;
            }
            case 2: {
                Intent intent = new Intent(mContext, LiuhecaiDetailActivity.class);
                intent.putExtra("code", code);
                mContext.startActivity(intent);
                break;
            }
            case 3: {
                Intent intent = new Intent(mContext, ChuantongcaiActivity.class);
                intent.putExtra("code", code);
                mContext.startActivity(intent);
                break;
            }
        }
    }

    public void setOnMoreBuyLotteryClickListener(MoreBuyLotteryClickListener listener) {
        this.listener = listener;

    }

    public interface MoreBuyLotteryClickListener {
        void onClick(GetWholeCategoryResponse data);
    }
}
